package com.itheima.service.db.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.domain.vo.AnalysisSummaryVo;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisService;
import com.itheima.util.ComputeUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    //统计数据 添加一条日志分析
    @Override
    public void logToAnalysisByDay() {
        //1.判断数据库有没有今天的日志分析数据 如果有执行修改 如果没有执行添加
        String today = DateUtil.today();//今天  offsetDay 日期偏移
        String yesterday = DateUtil.offsetDay(new Date() , -1).toDateStr();//昨天

        //2.查询数据
        //2.1 查询今日注册
        Integer registerNumber = logMapper.findByLoginOrReg("0102", today);
        //2.2 查询今日登录
        Integer loginNumber = logMapper.findByLoginOrReg("0101", today);
        //2.3 查询活跃用户
        Integer activeNumber = logMapper.findActiveByLogTime(today);
        //2.4 查询次日留存
        Integer retentionNumber = logMapper.findRetention(yesterday, today);

        //3.构建数据分析的对象  判断数据库有没有今天的日志分析数据 如果有执行修改 如果没有执行添加
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("record_date" , today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(queryWrapper);
        if(analysisByDay == null){//刚过凌晨 还没有产生今天的数据  需要新增
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate( new Date());//统计时间
            analysisByDay.setNumRegistered(registerNumber);//今日注册
            analysisByDay.setNumActive( activeNumber ); //活跃
            analysisByDay.setNumLogin(loginNumber); //登录
            analysisByDay.setNumRetention1d(retentionNumber); //次日留存
            analysisByDayMapper.insert( analysisByDay );//保存
        }else{ // 数据库已经存在记录 只需要修改即可
            analysisByDay.setNumRegistered(registerNumber);//今日注册
            analysisByDay.setNumActive( activeNumber ); //活跃
            analysisByDay.setNumLogin(loginNumber); //登录
            analysisByDay.setNumRetention1d(retentionNumber); //次日留存
            analysisByDayMapper.updateById(analysisByDay);//修改操作
        }

    }


    /**
     * 1.先创建对象
     * 2.赋值对象
     * 3.根据赋值的对象 里面的属性 开始找数据
     * @return
     */
    //首页统计  AnalysisSummaryVo 有九个参数
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        //1.创建对象
        AnalysisSummaryVo vo = new AnalysisSummaryVo();

        //4.查询数据的来源
        String today = DateUtil.today();//今天  offsetDay 日期偏移
        String yesterday = DateUtil.offsetDay(new Date() , -1).toDateStr();//昨天
        //4.1 查询今天的数据
        AnalysisByDay todayAnalysisByDay = analysisByDayMapper.findByDateTime(today);
        //4.2 查询昨天的数据
        AnalysisByDay yesterdayAnalysisByDay = analysisByDayMapper.findByDateTime(yesterday);


        //累计用户
        Long cumulativeUsers = analysisByDayMapper.findCountSumRegister();

        //七天之前的日期
        String weekDate = DateUtil.offsetDay(new Date() , -7).toDateStr();//一周前
        Long weekActiveCount = analysisByDayMapper.findCountSubActive(weekDate, today);

        //30天以前
        String monthDate = DateUtil.offsetDay(new Date() , -30).toDateStr();//一个月前
        Long monthActiveCount = analysisByDayMapper.findCountSubActive(monthDate, today);
        //3.封装对象
        vo.setCumulativeUsers(cumulativeUsers);//累计用户
        vo.setActivePassMonth(monthActiveCount);//过去三十天活跃用户
        vo.setActivePassWeek(weekActiveCount); //过去七天活跃用户

        vo.setNewUsersToday(todayAnalysisByDay.getNumRegistered().longValue());//今日新增用户
        vo.setLoginTimesToday(todayAnalysisByDay.getNumLogin().longValue());//今日登录次数
        vo.setActiveUsersToday(todayAnalysisByDay.getNumActive().longValue());//今日活跃用户

        //项目里已经提供好了计算的方法
        BigDecimal bigDecimal1 = ComputeUtil.computeRate(todayAnalysisByDay.getNumRegistered(), yesterdayAnalysisByDay.getNumRegistered());
        vo.setNewUsersTodayRate(bigDecimal1);//今日新增用户环比

        BigDecimal bigDecimal2 = ComputeUtil.computeRate(todayAnalysisByDay.getNumLogin(), yesterdayAnalysisByDay.getNumLogin());
        vo.setLoginTimesTodayRate(bigDecimal2);//今日登录次数环比

        BigDecimal bigDecimal3 = ComputeUtil.computeRate(todayAnalysisByDay.getNumActive(), yesterdayAnalysisByDay.getNumActive());
        vo.setActiveUsersTodayRate(bigDecimal3);//今日活跃用户环比
        //2.返回对象
        return vo;
    }
}
